﻿Public Class PagerControl
#Region "分页字段和属性"
    ''' <summary>
    ''' 当前页面
    ''' </summary>
    Public Overridable Property PageIndex As Integer = 1
    ''' <summary>
    ''' 每页记录数
    ''' </summary>
    Public Overridable Property PageSize As Integer = 100
    ''' <summary>
    ''' 总记录数
    ''' </summary>
    Public Overridable Property RecordCount As Integer = 0
    Private m_pageCount As Integer = 0
    ''' <summary>
    ''' 总页数
    ''' </summary>
    Public ReadOnly Property PageCount As Integer
        Get
            If PageSize <> 0 Then
                m_pageCount = GetPageCount()
            End If
            Return m_pageCount
        End Get
    End Property
    Private m_jumpText As String
    ''' <summary>
    ''' 跳转按钮文本
    ''' </summary>
    Public Property JumpText() As String
        Get
            If String.IsNullOrEmpty(m_jumpText) Then
                m_jumpText = "Go"
            End If
            Return m_jumpText
        End Get
        Set(value As String)
            m_jumpText = value
        End Set
    End Property
#End Region

#Region "页码变化触发事件"
    Public Event OnPageChanged()
#End Region

#Region "分页及相关事件功能实现"
    Private Sub SetFormCtrEnabled()
        lnkFirst.Enabled = True
        lnkPrev.Enabled = True
        lnkNext.Enabled = True
        lnkLast.Enabled = True
        btnGo.Enabled = True
    End Sub

    Private Function GetPageCount() As Integer
        If PageSize = 0 Then
            Return 0
        End If
        Dim pageCount As Integer = RecordCount / PageSize
        If RecordCount Mod PageSize = 0 Then
            pageCount = RecordCount / PageSize
        Else
            pageCount = RecordCount / PageSize + 1
        End If
        Return pageCount
    End Function

    ''' <summary>
    ''' 外部调用
    ''' </summary>
    Public Sub DrawControl(count As Integer)
        recordCount = count
        DrawControl(False)
    End Sub
    ''' <summary>
    ''' 页面控件呈现
    ''' </summary>
    Private Sub DrawControl(callEvent As Boolean)
        btnGo.Text = JumpText
        lblCurrentPage.Text = PageIndex.ToString()
        lblPageCount.Text = PageCount.ToString()
        lblTotalCount.Text = RecordCount.ToString()
        txtPageSize.Text = PageSize.ToString()

        If callEvent Then
            '当前分页数字改变时，触发委托事件
            RaiseEvent OnPageChanged()
        End If
        SetFormCtrEnabled()
        If PageCount = 1 Then
            '有且仅有一页
            lnkFirst.Enabled = False
            lnkPrev.Enabled = False
            lnkNext.Enabled = False
            lnkLast.Enabled = False
            btnGo.Enabled = False
        ElseIf PageIndex = 1 Then
            '第一页
            lnkFirst.Enabled = False
            lnkPrev.Enabled = False
        ElseIf PageIndex = PageCount Then
            '最后一页
            lnkNext.Enabled = False
            lnkLast.Enabled = False
        End If
    End Sub
#End Region

#Region "相关控件事件"
    Private Sub lnkFirst_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lnkFirst.LinkClicked
        PageIndex = 1
        DrawControl(True)
    End Sub

    Private Sub lnkPrev_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lnkPrev.LinkClicked
        PageIndex = System.Math.Max(1, PageIndex - 1)
        DrawControl(True)
    End Sub

    Private Sub lnkNext_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lnkNext.LinkClicked
        PageIndex = System.Math.Min(PageCount, PageIndex + 1)
        DrawControl(True)
    End Sub

    Private Sub lnkLast_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lnkLast.LinkClicked
        PageIndex = PageCount
        DrawControl(True)
    End Sub

    Private Sub txtPageNum_KeyDown(sender As Object, e As KeyEventArgs) Handles txtPageNum.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnGo.PerformClick()
        End If
    End Sub

    Private Sub txtPageNum_TextChanged(sender As Object, e As EventArgs) Handles txtPageNum.TextChanged
        Dim num As Integer = 0
        If Integer.TryParse(txtPageNum.Text.Trim(), num) AndAlso num > 0 Then
            If num > PageCount Then
                txtPageNum.Text = PageCount
            End If
        End If
    End Sub

    Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
        Dim num As Integer = 0
        If Integer.TryParse(txtPageNum.Text.Trim(), num) AndAlso num > 0 Then
            PageIndex = num
            DrawControl(True)
        End If
    End Sub
#End Region
    Dim IsTextChanged As Boolean = False

    Private Sub txtPageSize_Leave(sender As Object, e As EventArgs) Handles txtPageSize.Leave
        If IsTextChanged Then
            IsTextChanged = False
            lnkFirst_LinkClicked(lnkFirst, Nothing)
        End If
    End Sub

    Private Sub txtPageSize_TextChanged(sender As Object, e As EventArgs) Handles txtPageSize.TextChanged
        Dim num As Integer = 0
        If Not Integer.TryParse(txtPageSize.Text.Trim(), num) OrElse num <= 0 Then
            num = 100
            txtPageSize.Text = "100"
        Else
            IsTextChanged = True
        End If
        PageSize = num
    End Sub
End Class
